﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Util;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.iSystem;
using HiStaff.Domain.CO;
using HiStaff.Domain.Sys;
using HiStaff.Util.Enum;
using System.Collections;
namespace HiStaff.iTime
{
    public partial class SignList : FormBase
    {
        public SignList()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.AddBasicCommandBar();
            InitControl();
            LoadData();
            return base.FormLoad();
        }
        protected override bool SaveButtonClick()
        {
            CO_SIGN_LST objSave = new CO_SIGN_LST();
            ValueControlsToObject(objSave, this);
            if (Service.ExcuteStoreNonQuery("APRU_CO_SIGN_LST", objSave))
            {
                MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                LoadData();
                return true;
            }
            return false;
        }

        protected override bool DeleteButtonClick()
        {
            if (grdData.SelectedItems.Count > 0)
            {
                var row = grdData.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    if (Service.ExcuteStoreNonQuery("APRD_CO_SIGN_LST", row.GetRow().DataRow))
                    {
                        MessageBox.Show(GetMessageById(Constant.MSG_TRANSACTION_SUCCESSFUL), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        LoadData();
                        return true;
                    }
                }
            }
            return false;
        }

        protected override bool CancelButtonClick()
        {
            BindDataFromGrid();
            return true;
        }

        private void LoadData()
        {
            HiStaff.iSystem.Data.ALLCOSIGN = Service.TimeClient.GetAllSignList();
            grdData.DataSource = HiStaff.iSystem.Data.ALLCOSIGN;
            grdData.Refresh();
        }

        private void InitControl()
        {
            List<SY_DEFAULT_LIST> lstSource;

            List<SY_DATA_TYPE> lstDataType = ConvertHelper.DataTableToList<SY_DATA_TYPE>((DataTable)cbxDataType.DataSource);
            grdData.RootTable.Columns["DATATYPEID"].ValueList.PopulateValueList(lstDataType, "ID", "DESCRIPTION");


            lstSource = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>((DataTable)cbxDataMode.DataSource);
            grdData.RootTable.Columns["DATAMODEID"].ValueList.PopulateValueList(lstSource, "ID", "NAME");

            lstSource = ConvertHelper.DataTableToList<SY_DEFAULT_LIST>((DataTable)cbxSignGroup.DataSource);
            grdData.RootTable.Columns["GROUPID"].ValueList.PopulateValueList(lstSource, "ID", "NAME");
        }

        private void cbxSignGroup_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cbxSignGroup.SelectedIndex >= 0 &&
                (decimal)WorkSignGroup.ShiftSign == (decimal)cbxSignGroup.SelectedValue)
            {
                gbxInfoShift.Visible = true;
            }
            else
            {
                gbxInfoShift.Visible = false;
            }
        }
        private void BindDataFromGrid()
        {
            if (grdData.SelectedItems.Count > 0)
            {
                var row = grdData.SelectedItems[0];
                if (row.RowType == Janus.Windows.GridEX.RowType.Record)
                {
                    ValueObjectToControls(row.GetRow().DataRow, this);
                }
            }
        }
        private void grdData_SelectionChanged(object sender, EventArgs e)
        {
            BindDataFromGrid();
        }

    }
}
